package com.tbit.uqbike.webmanager.controller;

import com.tbit.uqbike.constant.OperaPswConstant;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.AccountUser;
import com.tbit.uqbike.object.pojo.RefundOpeLog;
import com.tbit.uqbike.object.pojo.User;
import com.tbit.uqbike.object.pojo.statistics.ExportRefundOpeLog;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.AccountService;
import com.tbit.uqbike.service.business.OperaPswService;
import com.tbit.uqbike.service.business.RefundOpeLogService;
import com.tbit.uqbike.webmanager.dao.core.AccountUserDao;
import com.tbit.uqbike.webmanager.dao.core.UserDao;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import com.tbit.uqbike.webmanager.util.excel.ExportExcelUtil;
import com.tbit.uqbike.webmanager.util.excel.ExportExcelWrapper;
import com.tbit.utils.DateTimeUtil;
import com.tbit.utils.DoubleUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 退款记录(RefundOpeLog)表控制层
 *
 * @author jing
 * @since 2020-11-20 15:47:22
 */
@RestController
@RequestMapping("/refundopelog")
public class RefundOpeLogController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private RefundOpeLogService RefundOpeLogService;
    @Resource
    private AccountUserDao accountUserDao;
    @Resource
    private UserDao userDao;
    @Resource
    private AccountService accountService;
    @Autowired
    private OperaPswService operaPswService;

    /**
     * 通过主键查询单条数据
     *
     * @param accountId 主键
     * @return 单条数据
     */
    @PostMapping("/selectOne")
    public JsonResult selectOne(Integer accountId) {
        return JsonResult.ok(this.RefundOpeLogService.queryById(accountId));
    }

    @PostMapping("/queryPage")
    public JsonResult queryPage(RefundOpeLog RefundOpeLog) {
        Assert.notNull(RefundOpeLog.getAccountId(), "ver.areaId.notNull");
        Assert.notNull(RefundOpeLog.getStartTime(), "sugar.startRideDt.notNull");
        Assert.notNull(RefundOpeLog.getEndTime(), "sugar.endRideDt.notNull");
        List<RefundOpeLog> list = RefundOpeLogService.queryPage(RefundOpeLog);
        Integer num = RefundOpeLogService.queryCount(RefundOpeLog);
        return JsonResult.ok(list, num);
    }


    /**
     * 退款记录导出
     * @param refundOpeLog 查询参数
     * @return fileName
     */
    @PostMapping("/export")
    public Result<Object> export(RefundOpeLog refundOpeLog) {
        Assert.notNull(refundOpeLog.getAccountId(), "ver.areaId.notNull");
        Assert.notNull(refundOpeLog.getStartTime(), "sugar.startRideDt.notNull");
        Assert.notNull(refundOpeLog.getEndTime(), "sugar.endRideDt.notNull");
        List<ExportRefundOpeLog> exports = RefundOpeLogService.export(refundOpeLog);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String fileName = String.format("%s~%s 退款记录", sdf.format(refundOpeLog.getStartTime()), sdf.format(refundOpeLog.getEndTime()));
        ExcelUtil<ExportRefundOpeLog> excelUtil = new ExcelUtil<>(ExportRefundOpeLog.class);
        JsonResult result = excelUtil.exportExcel(exports, fileName);
        return Result.success(result.getData());
    }

    @PostMapping("/insert")
    public JsonResult insert(RefundOpeLog RefundOpeLog, HttpServletRequest request) {
        Assert.notNull(RefundOpeLog.getAccountId(), "ver.areaId.notNull");
        Assert.notNull(RefundOpeLog.getUserPhone(), "sugar.phone.notNull");
        // 获取用户信息
        LoginInfo loginInfo = getLogin(request);
        /**操作密码校验*/
        if (!OperaPswConstant.ADMIN_OPERA_PSW.equals(RefundOpeLog.getOpeatePsw())) {
            throw new BaseException(SystemCodeEnum.OPERA_PAW_ERROR);
        }
        Map<String, Object> map = new HashMap<>();
        Integer brandId = accountService.getBrandId(RefundOpeLog.getAccountId());
        map.put("accountId", brandId);
        map.put("phone", RefundOpeLog.getUserPhone());
        User user = userDao.getByAreaId(map);
        if (null == user) {
            throw new BaseException(SystemCodeEnum.USER_NOT_EXIST_AREA);
        }
        RefundOpeLog.setUserId(user.getUserId());
        AccountUser accountUser = accountUserDao.getAccountUserById(loginInfo.getAccountUserId());
        RefundOpeLog.setAccountUserId(accountUser.getAccountUserId());
        RefundOpeLog.setAccountUserPhone(accountUser.getPhone());
        RefundOpeLog.setOperaTime(new Date());
        RefundOpeLogService.insert(RefundOpeLog);
        return JsonResult.ok();
    }

    @PostMapping("/queryByTime")
    public JsonResult queryByTime(RefundOpeLog RefundOpeLog) {
        Assert.notNull(RefundOpeLog.getAccountId(), "ver.areaId.notNull");
        Assert.notNull(RefundOpeLog.getStartTime(), "sugar.startRideDt.notNull");
        Assert.notNull(RefundOpeLog.getEndTime(), "sugar.endRideDt.notNull");
        List<Map<String, Object>> lists = RefundOpeLogService.queryByTime(RefundOpeLog);
        return JsonResult.ok(lists);
    }

    @GetMapping("/export")
    public Result export(RefundOpeLog RefundOpeLog, HttpServletResponse response) {
        Assert.notNull(RefundOpeLog.getAccountId(), "ver.areaId.notNull");
        Assert.notNull(RefundOpeLog.getStartTime(), "sugar.startRideDt.notNull");
        Assert.notNull(RefundOpeLog.getEndTime(), "sugar.endRideDt.notNull");
        List<ExportRefundOpeLog> exports = RefundOpeLogService.export(RefundOpeLog);
        ExcelUtil<ExportRefundOpeLog> excelUtil = new ExcelUtil<>(ExportRefundOpeLog.class);
        JsonResult jsonResult = excelUtil.exportExcel(exports, "退款统计详情");
        return Result.success(jsonResult.getData());
//        for (ExportRefundOpeLog export : exports) {
//            export.setMoney(Double.valueOf(export.getMoney())+"");
//        }
       /* ExportExcelWrapper<ExportRefundOpeLog> util = new ExportExcelWrapper<>();
        String filename = "退款统计详情" + DateTimeUtil.getNowTimeYMD();
        String[] columnNames = {"操作者名称", "操作者号码", "用户名", "用户号码", "金额", "备注", "操作时间", "类型"};
        util.exportExcel(filename, filename, columnNames, exports, response, ExportExcelUtil.EXCEL_FILE_2003);*/
    }

    @GetMapping("/exportByTime")
    public void exportByTime(RefundOpeLog RefundOpeLog, HttpServletResponse response) {
        Assert.notNull(RefundOpeLog.getAccountId(), "ver.areaId.notNull");
        Assert.notNull(RefundOpeLog.getStartTime(), "sugar.startRideDt.notNull");
        Assert.notNull(RefundOpeLog.getEndTime(), "sugar.endRideDt.notNull");
        List<Map<String, Object>> maps = RefundOpeLogService.queryByTime(RefundOpeLog);
        List<String[]> export = new ArrayList<>();
        export.add(new String[]{"日期", "金额", "次数"});
        maps.stream().forEach(map -> {
            String[] str = new String[3];
            str[0] = String.valueOf(map.get("time"));
            str[1] = "" + DoubleUtils.doubleScale(Integer.parseInt(String.valueOf(map.get("money"))) / 100.0);
            str[2] = String.valueOf(map.get("num"));
            export.add(str);
        });
        String filename = "退款日统计" + DateTimeUtil.getNowTimeYMD();
        ExportExcelWrapper<String[]> util = new ExportExcelWrapper<>();
        util.exportExcel(filename, filename, null, export, response, ExportExcelUtil.EXCEL_FILE_2003);
    }
}